home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-06-19 | 2.4 KB | 102 lines |
- /**
- * StockServlet receives the customer "id" on the
- * request parameter list. It queries the
- * customer database to get the list of stocks, then
- * queries the stock database for the price
- * of each stock. It then formats this info as WML.
- *
- * Note: Only the GET command is supported. No error
- * checking is included in this listing.
- *
- * From the Stock Quote Push Application chapter in the Nokia
- * Developer's Guide for the WAP Toolkit.
- */
-
- import java.io.*;
- import java.util.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
-
- public class StockServlet extends HttpServlet {
- // throws ServletException, IOTException {
-
- static private String TITLE = "Today's Stock Quotes";
-
- public void doGet(HttpServletRequest req, HttpServletResponse res) {
- //
- // Set response content type
- //
- res.setContentType("text/vnd.wap.wml");
- //
- // Get info from request
- //
- String id = req.getParameter("id");
- PrintWriter out = null;
- try {
- out = res.getWriter();
- } catch (Exception e) {
- return;
- }
- //
- // Query customer database for list of favorite stocks
- //
- Customer cust = CustomerDatabase.get(id);
- Vector list = cust.getStocks();
- //
- // Print WML preface, title, and table header
- //
- out.println("<wml><deck><card>");
- out.println( TITLE );
- //
- // start table
- //
- out.println( "<table columns=\"2\">");
- //
- // table header
- //
- out.println("<tr><td><b>Company</td>");
- out.println("<td><b>Stock Price</b></td></tr>");
- //
- // Iterate through stock list and query stock price
- // database for current price.
- //
- for (int i =0;i < list.size(); i++ ) {
- String price = StockInfoDB.getPrice( (String)list.elementAt(i));
- out.println( "<tr>");
- out.println( "<td>" + list.elementAt(i) + "</td>");
- out.println( "<td>" + price + "</td>");
- out.println( "</tr>");
- }
- //
- // Finish WML page.
- //
- out.println("</table></card></deck></wml>");
- }
- }
-
- /*
- * These are stub classes which would provide stock database info
- * and track customer info.
- */
- class Customer {
- private String id;
- public Customer(String id) {
- this.id = id;
- }
- public Vector getStocks() {
- Vector v = new Vector(1);
- v.add("FooCo");
- return v;
- }
- }
- class CustomerDatabase {
- public static Customer get (String id) {
- return new Customer(id);
- }
- }
- class StockInfoDB {
- public static String getPrice( String name ) {
- return "Stock price of " + name;
- }
- }
-